<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */

class GroupsController extends AppController {

    /**
     * Define $name
     *
     */
    var $name = 'Groups';
    /**
     * Define $helpers
     *
     */
    var $helpers = array('Html', 'Form','Javascript');

    /**
     * Describe index
     *
     * @param $committee
     * @return null
     */
    function index($committee) {
        $this->redirect(array('committee'=>$committee,'controller'=>'memberships','action'=>'index'));
       /* $this->Group->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
        
        $this->set('dcommittee',$this->dcommittee);
        $this->Group->recursive = 0;
        // first page
        if (empty($this->params['named']['page']) || empty($this->data)) $this->Session->del('Search.name');  

        // get the search terms
        if(!empty($this->data['Search']['name'])) $cursearch_group = $this->data['Search']['name'];
        elseif($this->Session->check('Search.name')) $cursearch_group = $this->Session->read('Search.name'); 
        else $cursearch_group = '';

        // construct queries
        $filters = array('committee_id'=>$this->dcommittee['Committee']['id']);
        if(isset($cursearch_group) && $cursearch_group!='') {
            array_push($filters,"Group.name  like '%".$cursearch_group."%'");
            $this->Session->write('Search.name', $cursearch_group);        
        }

        $groups=$this->paginate('Group',$filters);
        $groups=$this->fixupauth($groups,'Group',$this->Auth->user('id'),$this->dcommittee['Committee']['id']);
        $this->set('groups', $groups);*/
    }

    /**
     * Describe view
     *
     * @param $committee
     * @param $id
     * @return null
     */
    function view($committee,$id = null) {
        $this->Group->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
        
        $this->set('dcommittee',$this->dcommittee);
        if (!$id) {
            $this->Session->setFlash(__('Invalid Group.', true));
            $this->redirect(array('committee'=>$this->dcommittee['Committee']['short_name'],'controller'=>'memberships','action'=>'index'));
        }
        $this->set('group', $this->Group->read(null, $id));
    }

    /**
     * Describe add
     *
     * @param $committee
     * @return null
     */
    function add($committee) {
        $this->Group->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
        if (!empty($this->data)) {
            $this->data['Group']['committee_id']=$this->dcommittee['Committee']['id'];
            $this->Group->create();
            if ($this->Group->save($this->data)) {
                $this->Session->setFlash(__('The Group has been saved', true));
                $this->redirect(array('controller'=>'memberships','action'=>'index','committee'=>$this->dcommittee['Committee']['short_name']));
            } else {
                $this->Session->setFlash(__('The Group could not be saved. Please, try again.', true));
            }
        }
        $users = $this->Group->User->getUsersPerCommitteeList($this->dcommittee['Committee']['id']);
        $this->set(compact('users'));
    }

    /**
     * Describe edit
     *
     * @param $committee
     * @param $id
     * @return null
     */
    function edit($committee,$id = null) {
        $this->Group->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
        
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Group', true));
            $this->redirect(array('controller'=>'memberships','action'=>'index','committee'=>$this->dcommittee['Committee']['short_name']));
        }
        if (!empty($this->data)) {
            if ($this->Group->save($this->data)) {
                $this->Session->setFlash(__('The Group has been saved', true));
                $this->redirect(array('controller'=>'memberships','action'=>'index','committee'=>$this->dcommittee['Committee']['short_name']));
            } else {
                $this->Session->setFlash(__('The Group could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Group->read(null, $id);
        }
        $users = $this->Group->User->getUsersPerCommitteeList($this->dcommittee['Committee']['id']);
        $this->set(compact('users'));
    }

    /**
     * Describe delete
     *
     * @param $committee
     * @param $id
     * @return null
     */
    function delete($committee,$id = null) {
        $dcommittee=$this->Group->Committee->findByShortName($committee);
        $this->set('dcommittee',$dcommittee);
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for Group', true));
            $this->redirect(array('controller'=>'memberships','action'=>'index','committee'=>$dcommittee['Committee']['short_name']));
        }
        $this->Group->delete($id);
        $this->Session->setFlash(__('Group deleted', true));
        $this->redirect(array('controller'=>'memberships','action'=>'index','committee'=>$dcommittee['Committee']['short_name']));
    }
}
?>
